package com.diandian.backend.dingding.data.service;

import com.diandian.base.boot.starter.exception.DefaultException;
import com.diandian.base.boot.starter.response.PageResult;
import com.diandian.backend.dingding.data.domain.DingdingStaffExtend;
import com.diandian.backend.dingding.data.dto.*;
import com.diandian.backend.dingding.data.mapper.DingdingStaffExtendMapper;
import com.diandian.backend.dingding.data.resp.DingdingStaffExtendDetailResp;
import com.diandian.backend.dingding.data.resp.DingdingStaffExtendPageResp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.diandian.backend.dingding.data.resp.DingdingStaffExtendListResp;
import java.util.*;
import com.google.common.collect.Lists;

@Service
public class DingdingStaffExtendService {

    @Autowired
    private DingdingStaffExtendMapper dingdingStaffExtendMapper;

    public PageResult page(DingdingStaffExtendPageParamDTO requestDTO){

        requestDTO.check();
        int pageNum = requestDTO.getPageNum();
        int pageSize = requestDTO.getPageSize();

        // 查询总数
        int totalCount = dingdingStaffExtendMapper.getCount(requestDTO);

        // 查询数据集合
        List<DingdingStaffExtend> dataList = dingdingStaffExtendMapper.getPage(requestDTO, requestDTO.getPageSize(), (requestDTO.getPageNum() - 1) * requestDTO.getPageSize());

        // 组装数据
        List<DingdingStaffExtendPageResp> respList = new ArrayList<>();
        if(dataList != null && dataList.size() > 0){
            dataList.stream().forEach( x ->{
                DingdingStaffExtendPageResp item = new DingdingStaffExtendPageResp();
                item.setId(x.getId());
                item.setBelong(x.getBelong());
                item.setCreateUserId(x.getCreateUserId());
                item.setCreateUserName(x.getCreateUserName());
                item.setCreateTime(x.getCreateTime());
                item.setOrgId(x.getOrgId());
                item.setOrgCode(x.getOrgCode());
                item.setUpdateUserId(x.getUpdateUserId());
                item.setUpdateUserName(x.getUpdateUserName());
                item.setUpdateTime(x.getUpdateTime());
                item.setDel(x.getDel());
                item.setUserId(x.getUserId());
                item.setStar(x.getStar());
                item.setXinghuoTier(x.getXinghuoTier());
                item.setRecommendBrand(x.getRecommendBrand());
                item.setStatus(x.getStatus());
             respList.add(item);
            });
        }

        return new PageResult(pageNum, pageSize, totalCount, respList);
    }

    public List<DingdingStaffExtendListResp> getList(DingdingStaffExtendListParamDTO requestDTO){

        // 查询数据集合
        List<DingdingStaffExtend> dataList = dingdingStaffExtendMapper.getList(requestDTO);

        // 组装数据
        List<DingdingStaffExtendListResp> respList = new ArrayList<>();
        if(dataList != null && dataList.size() > 0){
            dataList.stream().forEach( x ->{
                DingdingStaffExtendListResp item = new DingdingStaffExtendListResp();
                item.setId(x.getId());
                item.setBelong(x.getBelong());
                item.setCreateUserId(x.getCreateUserId());
                item.setCreateUserName(x.getCreateUserName());
                item.setCreateTime(x.getCreateTime());
                item.setOrgId(x.getOrgId());
                item.setOrgCode(x.getOrgCode());
                item.setUpdateUserId(x.getUpdateUserId());
                item.setUpdateUserName(x.getUpdateUserName());
                item.setUpdateTime(x.getUpdateTime());
                item.setDel(x.getDel());
                item.setUserId(x.getUserId());
                item.setStar(x.getStar());
                item.setXinghuoTier(x.getXinghuoTier());
                item.setRecommendBrand(x.getRecommendBrand());
                item.setStatus(x.getStatus());
                respList.add(item);
            });
        }

        return respList;
    }

    public DingdingStaffExtendDetailResp getDetail(DingdingStaffExtendDetailParamDTO param) {

        param.check();

        DingdingStaffExtend dbData = dingdingStaffExtendMapper.getDetail(param);
        if(dbData == null){
            return null;
        }

        DingdingStaffExtendDetailResp result = new DingdingStaffExtendDetailResp();
        result.setId(dbData.getId());
        result.setBelong(dbData.getBelong());
        result.setCreateUserId(dbData.getCreateUserId());
        result.setCreateUserName(dbData.getCreateUserName());
        result.setCreateTime(dbData.getCreateTime());
        result.setOrgId(dbData.getOrgId());
        result.setOrgCode(dbData.getOrgCode());
        result.setUpdateUserId(dbData.getUpdateUserId());
        result.setUpdateUserName(dbData.getUpdateUserName());
        result.setUpdateTime(dbData.getUpdateTime());
        result.setDel(dbData.getDel());
        result.setUserId(dbData.getUserId());
        result.setStar(dbData.getStar());
        result.setXinghuoTier(dbData.getXinghuoTier());
        result.setRecommendBrand(dbData.getRecommendBrand());
        result.setStatus(dbData.getStatus());
        return result;
    }

    public void updateById(DingdingStaffExtendUpdateByIdParamDTO param) {
        param.check();

        DingdingStaffExtendDetailParamDTO queryDetailDTO = new DingdingStaffExtendDetailParamDTO();
        queryDetailDTO.setId(param.getId());
        queryDetailDTO.setBelong(param.getBelong());
        DingdingStaffExtend dbData = dingdingStaffExtendMapper.getDetail(queryDetailDTO);
        if(dbData == null){
            throw new DefaultException("更新的记录不存在");
        }

        dingdingStaffExtendMapper.updateById(param);
    }

    public void batchUpdateById(List<DingdingStaffExtendUpdateByIdParamDTO> dataList) {
        if(dataList == null || dataList.size() == 0){
            return;
        }
        dataList.forEach( x -> {
           x.check();
        });

        List<List<DingdingStaffExtendUpdateByIdParamDTO>> batchList = Lists.partition(dataList, 100);

        for (int i = 0; i < batchList.size(); i++) {
            dingdingStaffExtendMapper.batchUpdateById(batchList.get(i));
        }
    }

    public void deleteById(DingdingStaffExtendDeleteByIdParamDTO paramDTO) {
        paramDTO.check();
        dingdingStaffExtendMapper.deleteById(paramDTO);
    }

    /**
     * 同步：有则更新，无则新增
     * @param param
     */
    public void sync(DingdingStaffExtendSyncParamDTO param) {

        param.check();

        DingdingStaffExtendDetailParamDTO queryDetailDTO = new DingdingStaffExtendDetailParamDTO();
        queryDetailDTO.setUserId(param.getUserId());
        queryDetailDTO.setBelong(param.getBelong());
        DingdingStaffExtendDetailResp extendDetailResp = getDetail(queryDetailDTO);

        if(extendDetailResp == null){
            add(param.toDingdingStaffExtendAddParamDTO());
            return;
        }

        updateById(param.toDingdingStaffExtendUpdateByIdParamDTO(extendDetailResp.getId()));
    }

    public void add(DingdingStaffExtendAddParamDTO param) {
        if(param == null){
            return;
        }
        param.setDel(0);
        param.setCreateTime(new Date());
        dingdingStaffExtendMapper.add(param);
    }

    public void batchAdd(List<DingdingStaffExtendAddParamDTO> dataList) {
        if(dataList == null || dataList.size() == 0){
            return;
        }

        dataList.forEach(x -> {
            x.setDel(0);
            x.setCreateTime(new Date());
        });

        List<List<DingdingStaffExtendAddParamDTO>> batchList = Lists.partition(dataList, 100);

        for (int i = 0; i < batchList.size(); i++) {
            dingdingStaffExtendMapper.batchAdd(batchList.get(i));
        }
    }
}
